package cn.mihoyo.tenement.controller;

import cn.mihoyo.base.util.AjaxResult;
import cn.mihoyo.base.util.PageList;
import cn.mihoyo.tenement.domain.Renting;
import cn.mihoyo.tenement.query.RentingQuery;
import cn.mihoyo.tenement.service.RentingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/renting")
public class RentingController {
    @Autowired
    private RentingService rentingService;

    @GetMapping
    public List<Renting> findAll(){
        return rentingService.findAll();
    }

    @GetMapping("/{id}")
    public Renting findById(@PathVariable("id") Long id){
        return rentingService.findById(id);
    }

    @DeleteMapping("/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            rentingService.delete(id);
            return AjaxResult.me();
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.fail();
        }
    }

    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Renting Renting){
        try {
            if(Renting.getRtId() == null || Renting.getRtId() == 0){
                rentingService.add(Renting);
            }else {
                rentingService.update(Renting);
            }
            return AjaxResult.me();
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.fail();
        }
    }

    @PostMapping
    public PageList queryByPage(@RequestBody RentingQuery query){
        return rentingService.queryByPage(query);
    }
}
